package com.hncboy.chatgpt.front.mongodb;

import cn.hutool.json.JSONUtil;
import com.hncboy.chatgpt.base.domain.mongodb.VerificationCode;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

@Slf4j
@Component
public class VerificationCodeMapper {

    @Resource
    MongoTemplate mongoTemplate;

    public List<VerificationCode> selectByEmail(String email){
        Query query = new Query(Criteria.where("email").is(email));
        return mongoTemplate.find(query,VerificationCode.class);
    }

    public VerificationCode selectByEmailAndCode(String email,String code){
        Query query = new Query(Criteria.where("email")
                .is(email).and("context").is(code).and("status").is(true));
        return mongoTemplate.findOne(query,VerificationCode.class);
    }

    public VerificationCode selectByEmailFirst(String email){
        Query query = new Query(Criteria.where("email")
                .is(email).and("status").is(true));
        query.with(Sort.by(Sort.Order.desc("create_date"))).limit(1);
        return mongoTemplate.findOne(query,VerificationCode.class);
    }
    

    public VerificationCode insert(VerificationCode verificationCode){
        return mongoTemplate.insert(verificationCode);
    }

    public DeleteResult remove(VerificationCode verificationCode){
        Query query = new Query(Criteria.where("email")
                .is(verificationCode.getEmail())
                .and("context").is(verificationCode.getContext()));
        return mongoTemplate.remove(query,VerificationCode.class);
    }

    public Long upsert(VerificationCode verificationCode) {
        Query query = new Query(Criteria.where("email")
        		.is(verificationCode.getEmail()));
        Update update = objToUpdate(verificationCode);
        UpdateResult result = mongoTemplate.upsert(query, update, VerificationCode.class);
        return result.getMatchedCount();
    }

    private Update objToUpdate(VerificationCode po) {
        Update update = new Update();
        Class<?> clazz = po.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            try {
                if(null != field.get(po)){
                    update.set(fieldName, field.get(po));
                }
            } catch (IllegalAccessException e) {
                log.error("objToUpdate param:{}", JSONUtil.toJsonStr(po), e);
            }
        }
        return update;
    }
    
}
